package main

import (
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
	"kratos_cron_task/internal/boot"
	"kratos_cron_task/internal/conf"
	"kratos_cron_task/pkg/task1"
)

// Notice 自己实现一个跟 http与grpc 同级别的定时任务的server
// Notice 项目初始化创建 kratos的App的时候 将定时任务的server加到Server方法中～
func newApp(conf *conf.Server, logger log.Logger, httpServer *http.Server, grpcServer *grpc.Server, taskServer *task1.Server) *kratos.App {
	return kratos.New(
		kratos.ID(conf.Id),
		kratos.Name(conf.Name),
		kratos.Version(conf.Version),
		kratos.Metadata(map[string]string{}),
		kratos.Logger(logger),
		kratos.Server(
			taskServer,
			httpServer,
			grpcServer,
		),
	)
}

func main() {
	bootstrapConf := boot.NewBootConf().Run()
	bootLogger := boot.NewBootLog(bootstrapConf).Run()
	boot.NewBootTrace(bootstrapConf).Run()
	app, cleanup, err := wireApp(bootstrapConf.Server, bootstrapConf.Data, bootLogger, bootstrapConf)
	if err != nil {
		panic(err)
	}

	defer cleanup()
	if err := app.Run(); err != nil {
		panic(err)
	}
}
